from scrapy.spider import BaseSpider
from scrapy.selector import Selector

from spider.items import SpiderItem
import MySQLdb

class BitcarmodeltestSpider(BaseSpider):

    name = "bitcarm"
    conn = MySQLdb.connect(host="localhost",user="spider",passwd="Xjk@4047218?",db="spider")
    cursor = conn.cursor() 
    sql = "select url from sp_ba_carstyle order by id asc"
    result = cursor.execute(sql)
    carstyleUrlList = []

    for row in cursor.fetchall():
        carstyleUrlList.append(row[0])
    cursor.close()

    start_urls = carstyleUrlList
    #start_urls =['http://car.bitauto.com/xinaodia6l/'] 

    def parse(self,response):
        sel = Selector(response)
        areas = sel.xpath('//table[@id="compare_sale"]//tr')
        items = []
        for area in areas:
            item = SpiderItem() 
            item["carmodel"] = area.xpath('.//td[1]//div[@class="pdL10"]//a[1]/text()').extract()
            item["carmodelurl"] = area.xpath('.//td[1]//div[@class="pdL10"]//a[1]/@href').extract()
            temp = area.xpath('.//td[1]//div[@class="pdL10"]//span[1]/text()').extract()
            if len(temp) != 0:
                item["carmodelstate"] = 1 
            else:
                item["carmodelstate"] = 0
            item["mark"] = area.xpath('.//td[4]//span[1]/text()').extract()
            item["c1"] = response.url
            item["type"] = "bitcarmodel"
            if len(item["carmodel"]) == 0:
                del item
            else:
                items.append(item)
        return items
